package com.dataextend.controller;

import com.dataextend.entity.*;
import com.dataextend.service.*;
import com.dataextend.utils.TreeNodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(value = "菜单表接口")
@RestController
@RequestMapping("/api/menus")
@CrossOrigin(origins={"http://localhost:8080"},allowCredentials="true")
public class RootMenusController extends RootBaseApiController {

    @Autowired
    private MenusService menusService;
    @Autowired
    private RoleMenusService roleMenusService;

    @ApiOperation("添加")
    @RequiresPermissions("menus:add")
    @PostMapping("/add")
    public Map<String, Object> add(String name,
                                   String url,
                                   String icon,
                                   Long parentId)
    {
        MenusEntity menusEntity = new MenusEntity()
                .setName(name)
                .setUrl(url)
                .setIcon(icon)
                .setParentId(parentId)
                .setExpand(0);

        if (parentId != 0) {
            // 如果父级id!=0，说明拓展了，去查询父级id的拓展状态是否改变了，没有就修改，改变了就不修改
            if (menusService.queryById(parentId).getExpand() == 0) {
                menusService.update(new MenusEntity().setId(parentId).setExpand(1));
            }
        }

        menusService.add(menusEntity);

        return resp(200, "添加成功", menusEntity);
    }

    @ApiOperation("修改")
    @RequiresPermissions("menus:update")
    @PostMapping("/update")
    public Map<String, Object> update(Long id,
                                      String name,
                                      String url,
                                      String icon,
                                      Long parentId,
                                      Integer expand) {
        MenusEntity menusEntity = new MenusEntity()
                .setId(id)
                .setName(name)
                .setUrl(url)
                .setIcon(icon)
                .setParentId(parentId)
                .setExpand(expand);


        return resp(200, "修改成功", menusService.update(menusEntity));
    }

    @ApiOperation("删除")
    @RequiresPermissions("menus:delete")
    @PostMapping("/delete")
    public Map<String, Object> deleteById(Long id) {
        /*
        * 先查询是不是父节点id
        * 如果是父节点id
        * 查询父级id下的所有子节点id
        * for循环删除所有子节点id
        * 根据子节点id，去用户菜单表删除所有与此节点id有关信息
        * */
        MenusEntity menusEntity = menusService.queryById(id);
        if (menusEntity.getParentId() == 0) {
            List<MenusEntity> list = menusService.list(new MenusEntity().setParentId(id));
            for (MenusEntity m: list) {
                menusService.delete(m.getId());
                List<RoleMenusEntity> list1 = roleMenusService.list(new RoleMenusEntity().setMenusId(m.getId()));
                for (RoleMenusEntity rm: list1) {
                    roleMenusService.delete(rm.getId());
                }
            }
        }

        /*
        * 判断在用户权限表内是否有相关信息
        * 不是父节点id，只需要删除该节点id
        * 然后去角色菜单表删除所有与此节点id有关信息
        * */
        List<RoleMenusEntity> list = roleMenusService.list(new RoleMenusEntity().setMenusId(id));
        if (list.size() != 0) {
            for (RoleMenusEntity rm: list) {
                roleMenusService.delete(rm.getId());
            }
        }
        menusService.delete(id);
        return resp(200, "删除成功");
    }

    @ApiOperation("根据id查询")
    @GetMapping("/query")
    public Map<String, Object> queryById(Long id) {
        return resp(200, "查询成功", menusService.queryById(id));
    }

    @ApiOperation("查询所有")
    @GetMapping("/list")
    public Map<String, Object> list(Long id,
                                    String name,
                                    String url,
                                    String icon,
                                    Long parentId,
                                    Integer expand) {
        MenusEntity menusEntity = new MenusEntity()
                .setId(id)
                .setName(name)
                .setUrl(url)
                .setIcon(icon)
                .setParentId(parentId)
                .setExpand(expand);

        return resp(200, "查询所有成功", menusService.list(menusEntity));
    }

    @ApiOperation("查询所有菜单树信息")
    @RequiresPermissions("menus:list:menu")
    @GetMapping("/list/menu")
    public Map<String, Object> listMenu() {

        TreeNodeUtils treeNodeUtils = new TreeNodeUtils();

        List<Object> menusList = new ArrayList<>();  // 定义一个总集合

        // 查询第一层数据，然后遍历
        List<MenusEntity> menusListOne = menusService.list(new MenusEntity().setParentId(0L));
        for (MenusEntity m1: menusListOne) {
            // 根据第一层的父级id查询第二层的节点
            List<MenusEntity> menusListTwo = menusService.list(new MenusEntity().setParentId(m1.getId()));
            // 判断，如果第二层的集合不为空说明有节点，把它取出来，添加到父级节点下面
            if (menusListTwo.size() != 0) {
                List<Object> menusListTwoData = new ArrayList<>();  // 定义一个集合，用来存储第二层的集合
                // 循环添加节点
                for (MenusEntity m2: menusListTwo) {
                    menusListTwoData.add(treeNodeUtils.tree(m2.getId(), m2.getName(), m2.getParentId(), ""));
                }
                // 然后再添加到父节点
                menusList.add(treeNodeUtils.tree(m1.getId(), m1.getName(), m1.getParentId(), menusListTwoData));
            } else {
                menusList.add(treeNodeUtils.tree(m1.getId(), m1.getName(), m1.getParentId(), ""));
            }
        }

        return resp(200, "查询所有成功", menusList);
    }
}
